define(function(require, exports, module){
	'use strict';
	
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	require("resource/js/component/rules.js");
	var easyutil = require("resource/js/component/easyutil.js");
	var combobox = require("resource/js/component/easycombobox.js");
	var cols_data_config = undefined;
	var table_index = 0;
	var adds = {index:0,tableName:"",row:{},operType:"",operTypeName:"",isMore:"",isMoreName:""};
	var waitdatagird ;
	function initPage(){
		$("#tabsd").height($("#cc").height()-$(".tabseach").height()-60);
		easyutil.ajaxHandler(BASEPATH+'/act/actExTable/queryActExTableList.htm',{businessId:$("#businessId").val()},function(code,msg,data){
			if(data!=undefined&&data!=null&&data.length>0){
				combobox.init({ ele:"#cols_sel_tableKey",valueField:'tableKey',remote:false, textField:'shortNameCh',onChange:function(rec){
					waitdatagird.pager();
				},onLoadSuccess:function(){
					$("#cols_sel_tableKey").combobox('setValue',data[0].tableKey);
					$("#cols_sel_tableKey").combobox('setText',data[0].shortNameCh);
					waitdatagird = new easyutil.ajaxDataGrid(cols_data_config);
				}}).load(data);
			}else{
				$.messager.confirm('','当前业务未配置表，即将关闭页面。',function(r){
				    $("#colsdlg").dialog("close");
				});
				
			}
		});
		easyutil.ajaxHandler(BASEPATH+'/base/authoriza/conmonData/queryData.htm',{queryId:"basecode",param:"formOperType"},function(code,msg,data){
			combobox.init({ ele:"#cols_sel_operType",valueField:'id',remote:false, textField:'text',onLoadSuccess:function(){
				$("#cols_sel_operType").combobox("setValue",data[0].id);
			}}).load(data);
		});
		easyutil.ajaxHandler(BASEPATH+'/base/authoriza/conmonData/queryData.htm',{queryId:"basecode",param:"itemiskey"},function(code,msg,data){
			combobox.init({ ele:"#cols_sel_isMore",valueField:'id',remote:false, textField:'text',onLoadSuccess:function(){
				$("#cols_sel_isMore").combobox("setValue",1);
			}}).load(data);
		});
		$("#dld_cols_add").bind('click',function(){
			using(["form"],function(){
				if($("#colsform").form("validate")){
					var rows  = $("#colsDg").datagrid("getSelections");
					if(rows==undefined||rows==null||rows==''){
						$.messager.alert('','请选择表字段。');
					}else{
						if(edit==true){
							edit = false;
							$("#cols").html("");
						}
						var tableKey = $("#cols_sel_tableKey").combobox('getValue');
						var tableName = $("#cols_sel_tableKey").combobox('getText');
						tableName = tableKey+"("+tableName+")";
						var operType = $("#cols_sel_operType").combobox('getValue');
						var isMore = $("#cols_sel_isMore").combobox('getValue');
						var isMoreName = $("#cols_sel_isMore").combobox('getText');
						var operTypeName = $("#cols_sel_operType").combobox('getText');
						for(var j in rows){
							adds.index = 0;
							adds.tableName = tableName;
							adds.row = rows[j];
							adds.operType = operType;
							adds.operTypeName = operTypeName;
							adds.isMore = isMore;
							adds.isMoreName = isMoreName;
							addCols();
						}
					}
				}
			});
		});
	}
	function addCols(){
		var table_flag = {flag:false,index:-1};
		var col_flag = false;
		$(".head-tr").each(function(i,e){
			var table = JSON.parse($(e).find("td").first().text());
			if(table.tableKey == adds.row.tableKey){
				var tb = $(e).attr("tb");
				table_flag.flag = true;
				table_flag.index = tb;
				$(e).nextAll().each(function(j,e){
					var col = JSON.parse($(e).find("td").first().text());
					if(col.colKey==adds.row.colKey){
						col_flag = true;
					}
				});
			}
			adds.index=adds.index+1;
		});
		if(table_flag.flag&&!col_flag){
			var cols = {colKey:adds.row.colKey,colName:adds.row.colName};
			var table_html = "<tr tb='"+table_flag.index+"'><td style='display:none'>"+JSON.stringify(cols)+"</td><td>"+adds.row.colName+"</td><td>"+adds.row.colNameCh+"</td><td><a href='javascript:void(0);' class='delete-row'>删除</a></td></tr>";
			$(".head-tr").each(function(index,e){
				if($(e).attr("tb")==table_flag.index){
					$(e).after(table_html);
				}
			});
		}else if(!table_flag.flag){
			var table_data = {tableKey:adds.row.tableKey,operType:adds.operType,isMore:adds.isMore,cols:[]};
			var table_html ="<tr class='head-tr' tb='"+adds.index+"'><td style='display:none'>"+JSON.stringify(table_data)+"</td><td>表名："+adds.tableName+"</td><td>操作类型："+adds.operTypeName+"</td><td>是否多条："+adds.isMoreName+"</td></tr>"
			$(".table-col-row").append(table_html);
			addCols();
		}else{
			$.messager.alert('','表字段“'+adds.row.colName+"("+adds.row.colNameCh+')”选择重复！');
		}
		$(".delete-row").bind('click',function(e){
			if($(this).parent().parent().prev("tr").attr("class")=='head-tr' 
				&& ($(this).parent().parent().next("tr").attr("class")=='head-tr'
				|| 	$(this).parent().parent().next("tr").length == 0
				)){
					var tb = $(this).attr('tb');
					$(this).parent().parent().prev().remove();
			}
			$(this).parent().parent().remove();
		});
	}
	module.exports = {
		trans:function(id){
			var buttons ={ 
					title:{
						_class:"",
						text:"节点表单字段选择"
					},
					btn:[
				       {
					iconCls: 'icon-save',
					url:'xxxx',
					text:"保存",
					addClass:'l-btn-display',
					newRows:true,
					handler: function(){}
				}
				]};
			cols_data_config = {
				fitColumns:true,
				singleSelect:false,
				striped:true,
				autoRowHeight:false,
				fit: true,
				url:BASEPATH+'/act/actExTableCols/queryActExTableCols.htm',
				toolbar:buttons,
				columns:[[
					{field:'colKey',title:'列编码',width:100,align:'center',height:50,hidden:true},
					{field:'tableKey',title:'表编码',width:100,align:'center',height:50,hidden:true},
					{field:'colName',title:'列名',width:100,align:'center',height:50},
					{field:'colNameCh',title:'说明信息',width:100,align:'center',height:50},
			         {field:'colType',title:'类型',width:100,align:'center',height:50},
			         {field:'colLength',title:'长度',width:100,align:'center',height:50}
					]],
				eid:"colsDg",
				args:"colsform",
				pagination:true
			};
			using(["datagrid","parser","dialog","combobox","messager"],function(){
				initPage();
			});
		}
	}
});